package cc.mrbird.alarm.service.impl;

import cc.mrbird.alarm.dao.AlarmInfoMapper;
import cc.mrbird.alarm.domain.AlarmInfo;
import cc.mrbird.alarm.service.AlarmInfoService;
import cc.mrbird.assets.domain.AssetMana;
import cc.mrbird.assets.domain.AssetType;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.common.util.TimeUtil;
import cc.mrbird.management.domain.ManagementInstitutions;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service("alarmInfoService")
public class AlarmInfoServiceImpl extends BaseService<AlarmInfo> implements AlarmInfoService {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AlarmInfoMapper alarmInfoMapper;

    @Override
    public List<AlarmInfo> findAlarmInfoList(AlarmInfo alarmInfo, QueryRequest request) {
        try {
            Example example = new Example(AlarmInfo.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotBlank(alarmInfo.getAlarmType())) {
                criteria.andCondition("alarmType=",alarmInfo.getAlarmType() );
            }
            if (StringUtils.isNotBlank(alarmInfo.getDeviceType())) {
                criteria.andCondition("deviceType=",alarmInfo.getDeviceType() );
            }
            if (StringUtils.isNotBlank(alarmInfo.getGrid())) {
                criteria.andCondition("grid=",alarmInfo.getGrid() );
            }
            if (StringUtils.isNotBlank(alarmInfo.getUnit())) {
                criteria.andCondition("unit=",alarmInfo.getUnit() );
            }
            example.setOrderByClause("createTime");
            return this.selectByExample(example);
        } catch (Exception e) {
            log.error("error", e);
            return new ArrayList<>();
        }
    }

    @Override
    @Transactional
    public void add(AlarmInfo alarmInfo) {
        this.save(alarmInfo);
    }

    @Override
    public List<AlarmInfo> findAllTypeAndId() {
        return alarmInfoMapper.findAllTypeAndId();
    }

    @Override
    public List<AlarmInfo> fingAllByIds(String ids,String nextTime) {
        List<AlarmInfo> list = new ArrayList<>();
        List<String> list1 = Arrays.asList(ids.split(","));
        for (String s : list1) {
            AlarmInfo alarmInfo = alarmInfoMapper.findByIdAndTime(Long.parseLong(s),nextTime);
            list.add(alarmInfo);
        }
        return list;
    }

    @Override
    public List<AlarmInfo> findAllByAlarmType() {
        return alarmInfoMapper.findAllByAlarmType();
    }

    @Override
    public List<AlarmInfo> fingAllByIdsAndType(String ids) {
        List<AlarmInfo> list = new ArrayList<>();
        List<String> list1 = Arrays.asList(ids.split(","));
        for (String s : list1) {
            AlarmInfo alarmInfo = alarmInfoMapper.findById(Long.parseLong(s));
            list.add(alarmInfo);
        }
        return list;
    }

}
